package miiteen.customsportal.notice.service;

import miiteen.customsportal.film.dao.FilmResourceDao;
import miiteen.customsportal.html.beans.HtmlContent;
import miiteen.customsportal.html.dao.HtmlContentDao;
import miiteen.customsportal.notice.beans.NoticeInfo;
import miiteen.customsportal.notice.dao.NoticeInfoDao;
import miiteen.data.dao.BaseDao;
import miiteen.data.service.impl.BaseServiceImpl;
import miiteen.hr.pubconfig.CurrentHrInfo;
import miiteen.hr.user.beans.UserUserInfo;
import miiteen.sys.login.bean.UserShiro;
import miiteen.sys.login.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Name: CustomsPortal_Notice_Info
 * Des: -Service
 * Author: lx
 * Date: 2020-07-06
 */
@Service("noticeInfoService")
public class NoticeInfoService extends BaseServiceImpl<NoticeInfo> {

    @Autowired
    private NoticeInfoDao noticeInfoDao;
    @Autowired
    private HtmlContentDao htmlContentDao;

    @Autowired
    private FilmResourceDao filmResourceDao;
    @Override
    protected BaseDao<NoticeInfo> getDao() {
        return noticeInfoDao;
    }

    public List<NoticeInfo> findListByMapPreviousNext(Map<String, Object> conditionMap) {
        return noticeInfoDao.findListByMapPreviousNext(conditionMap);
    }

    @Transactional
    public long save(NoticeInfo noticeInfo) {
        UserShiro.ShiroUser shiroUser = LoginUtil.getCurUser();
        UserUserInfo userInfo = CurrentHrInfo.getUserInfo(shiroUser);
        boolean isUpdate=noticeInfo.getId()>0;
        if (noticeInfo.getId() > 0) {
            noticeInfo.setUpdateId(userInfo.getAccountId());
            noticeInfo.setUpdator(userInfo.getTrueName());
            noticeInfo.setUpdateTime(new Date());
        } else {
            noticeInfo.setCreateId(userInfo.getAccountId());
            noticeInfo.setCreator(userInfo.getTrueName());
            noticeInfo.setCreateTime(new Date());
        }

        if(noticeInfo.getImgSrc()!=null && noticeInfo.getImgSrc().length>0){
            //判断当前是否是修改操作
            //现在正文的图片用富文本代替了，这里原先上传正文图片的地方，替换为存到预览图片picture里面
            noticeInfo.setPictures(noticeInfo.getImgSrc()[0]);
           /* if(noticeInfo.getId()>0){
                Map<String,Object> map=new HashMap<>();
                map.put("newsInfoId",noticeInfo.getId());
                String id="'"+noticeInfo.getId()+"'";
                filmResourceDao.deleteNewsInfoId(id);
            }
            for(int i=0;i<noticeInfo.getImgSrc().length;i++){
                FilmResource filmResource = new FilmResource();
                filmResource.setNewsInfoId(result);
                filmResource.setId(0);
                filmResource.setImage(noticeInfo.getImgSrc()[i]);
                filmResourceDao.save(filmResource);
            }*/
        }
        long result = noticeInfoDao.save(noticeInfo);
        if(result>0){
            HtmlContent htmlContent=new HtmlContent();
            htmlContent.setInfoId(noticeInfo.getId());
            htmlContent.setHtmlContent(noticeInfo.getHtmlContent());
            htmlContent.setType(3);
            Map<String,Object> map=new HashMap<>();
            map.put("infoId",noticeInfo.getId());
            List<HtmlContent> list= htmlContentDao.findListByMap(map);
            if(isUpdate){
                if(list.size()>0){
                    htmlContent.setId(list.get(0).getId());
                }
            }
            htmlContentDao.save(htmlContent);

        }

        return result;
    }


}